import sys
import codecs
import os
import matplotlib.pyplot as plt
import numpy as np

log_file= sys.argv[1]

accs_e, accs_v, accs_p, accs_i= [], [], [], []
f1_e, f1_v, f1_p, f1_i = [], [], [], []
print("log file:", log_file)
with codecs.open(log_file, 'r', encoding= u'utf-8',errors='ignore') as fr:
    for line in fr:
        if "#####init acc:" in line:
            s = line.split(":", 1)[-1].strip().strip("(").strip(")").split(",", 2)
            accs_i.append(float(s[0]))
            P_arr, R_arr, F_arr, cnt_arr = s[-1].strip("(").strip(")").split("]),")
            # precs.append(np.mean([float(item) for item in P_arr.strip().strip("array([").split(",")]))
            # recs.append(np.mean([float(item) for item in R_arr.strip().strip("array([").split(",")]))
            f1_i.append(np.mean([float(item) for item in F_arr.strip().strip("array([").split(",")]))
            # pos_cnt, nature_cnt, neg_cnt = [int(item) for item in cnt_arr.strip().strip("]").strip("array([").split(",")]
        if "#####expand_idxs acc:" in line:
            s = line.split(":", 1)[-1].strip().strip("(").strip(")").split(",", 2)
            accs_e.append(float(s[0]))
            P_arr, R_arr, F_arr, cnt_arr = s[-1].strip("(").strip(")").split("]),")
            # precs.append(np.mean([float(item) for item in P_arr.strip().strip("array([").split(",")]))
            # recs.append(np.mean([float(item) for item in R_arr.strip().strip("array([").split(",")]))
            f1_e.append(np.mean([float(item) for item in F_arr.strip().strip("array([").split(",")]))
            # pos_cnt, nature_cnt, neg_cnt = [int(item) for item in cnt_arr.strip().strip("]").strip("array([").split(",")]
        if "#####valid_idxs acc:" in line:
            s = line.split(":", 1)[-1].strip().strip("(").strip(")").split(",", 2)
            accs_v.append(float(s[0]))
            P_arr, R_arr, F_arr, cnt_arr = s[-1].strip("(").strip(")").split("]),")
            # precs.append(np.mean([float(item) for item in P_arr.strip().strip("array([").split(",")]))
            # recs.append(np.mean([float(item) for item in R_arr.strip().strip("array([").split(",")]))
            f1_v.append(np.mean([float(item) for item in F_arr.strip().strip("array([").split(",")]))
            # pos_cnt, nature_cnt, neg_cnt = [int(item) for item in cnt_arr.strip().strip("]").strip("array([").split(",")]
        if "#####pos_weights acc:" in line:
            s = line.split(":", 1)[-1].strip().strip("(").strip(")").split(",", 2)
            accs_p.append(float(s[0]))
            P_arr, R_arr, F_arr, cnt_arr = s[-1].strip("(").strip(")").split("]),")
            # precs.append(np.mean([float(item) for item in P_arr.strip().strip("array([").split(",")]))
            # recs.append(np.mean([float(item) for item in R_arr.strip().strip("array([").split(",")]))
            f1_p.append(np.mean([float(item) for item in F_arr.strip().strip("array([").split(",")]))
            # pos_cnt, nature_cnt, neg_cnt = [int(item) for item in cnt_arr.strip().strip("]").strip("array([").split(",")]

init_acc = np.mean(accs_i)
repeat_num = len(accs_e)
plt.xlim(0, repeat_num*3+3*3)
plt.ylim(0.6, 0.85)
plt.plot([0, repeat_num*3+3*3], [init_acc]*2, linestyle='--', color="r")
plt.text(2.3*repeat_num, init_acc+0.005, "init acc", color="r")
plt.text(-2.5, init_acc, "%2.4f"%init_acc, color="r")
plt.bar(list(np.arange(0.5, 0.5+repeat_num, 1)), accs_e, width=1.0, label='expand_idxs acc',
                                        color=(214/255.0, 213/255.0, 183/255.0))
plt.bar(list(np.arange(repeat_num+0.5+3, 0.5+3+2*repeat_num, 1)), accs_p, width=1.0, label='valid_idxs acc',
                                        color=(25/255.0, 202/255.0, 173/255.0))
plt.bar(list(np.arange(0.5+6+2*repeat_num, 0.5+6+3*repeat_num, 1)), accs_p, width=1.0, label='pos_weights acc',
                                        color=(244/255.0, 96/255.0, 108/255.0))
plt.legend()
plt.show()

